package com.weight.system.org.web;

import com.weight.system.org.PO.OrgInfo;
import com.weight.system.org.VO.OrgInfoVO;
import com.weight.system.org.service.OrgInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 组织机构信息表
 */
@RestController
@Slf4j
@RequestMapping("/orgInfo")
public class OrgInfoController {

	@Autowired
	private OrgInfoService orgInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询组织机构信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForOrgInfoList",method=RequestMethod.GET)
	public ModelAndView queryForOrgInfoList(){

		Map<Object, Object> map =new HashMap<>();
		map.put("standbyfield1", "1");
	 

		
		//2、查询组织机构信息表列表
		List<OrgInfo> orgInfoList = orgInfoService.findOrgInfoByT(map);
		log.info("orgInfoList={}",orgInfoList);
		List<OrgInfoVO> orgInfoVOList = orgInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("org/queryForOrgInfoList");
			modelAndView.addObject("orgInfoVOList",orgInfoVOList);
			return modelAndView;
	}

	/**
	 * 3.添加、修改组织机构信息表操作
	 * @param orgId
	 * @return
	 */
	@RequestMapping(value="/addOrgInfo")
	public ModelAndView addOrgInfo(@RequestParam(value="orgId",required=false) String orgId){
			//1、创建返回对象
			OrgInfoVO orgInfoVO = new OrgInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(orgId)) {
				Map<String, String> map=new HashMap<>();
				map.put("orgId", orgId);
				log.info("修改操作，查询ID：orgId={}",orgId);
				OrgInfo orgInfo = orgInfoService.findByOrgInfoId(map);
				this.changePoToVO(orgInfoVO,orgInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("org/addOrgInfo");
			modelAndView.addObject("orgInfoVO",orgInfoVO); 
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改组织机构信息表操作
	 * @param orgInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveOrgInfo")
	public JsonResultUtil saveOrgInfo(@Valid OrgInfoVO orgInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,orgInfoVO);
			
			//2.获取id
			String orgId=orgInfoVO.getOrgId();
			OrgInfo orgInfo=new OrgInfo();
			BeanUtils.copyProperties(orgInfoVO,orgInfo);
			this.changeVOToPO(orgInfoVO,orgInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(orgId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("orgId", orgId);
				log.info("修改操作，查询ID：orgId={}",orgId);
				//查询是否存在
				orgInfoService.findByOrgInfoId(map);
				try{
					orgInfo.setUpdateBy("000");
					orgInfoService.updateOrgInfo(orgInfo);
					log.info("更新操作成功：orgInfo={}",orgInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					orgInfo.setOrgId(CommonUtils.getUUId());
					orgInfo.setCreateBy("000");
					orgInfo.setUpdateBy("000");
					orgInfo.setStandbyfield1("1");
					orgInfoService.insertOrgInfo(orgInfo);
					log.info("添加操作成功：orgInfo={}",orgInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}
	/**
	 * 2.查询组织机构信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForOrgInfoListDG",method=RequestMethod.GET)
	public String queryForOrgInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String orgLname = request.getParameter("orgLname");
			String orgHname = request.getParameter("orgHname");
			String orgCode = request.getParameter("orgCode");
			String orgUser = request.getParameter("orgUser");
			String orgTel = request.getParameter("orgTel");
			String orgEmail = request.getParameter("orgEmail");
			String orgAdda = request.getParameter("orgAdda");
			String orgAddb = request.getParameter("orgAddb");
			String orgAddc = request.getParameter("orgAddc");
			String orgAddd = request.getParameter("orgAddd");
			String orgAdde = request.getParameter("orgAdde");
			String orgLon = request.getParameter("orgLon");
			String orgLat = request.getParameter("orgLat");
			String orgPcode = request.getParameter("orgPcode");
			String orgSort = request.getParameter("orgSort");

			Map<Object, Object> map =new HashMap<>();
			map.put("orgLname", orgLname);
			map.put("orgHname", orgHname);
			map.put("orgCode", orgCode);
			map.put("orgUser", orgUser);
			map.put("orgTel", orgTel);
			map.put("orgEmail", orgEmail);
			map.put("orgAdda", orgAdda);
			map.put("orgAddb", orgAddb);
			map.put("orgAddc", orgAddc);
			map.put("orgAddd", orgAddd);
			map.put("orgAdde", orgAdde);
			map.put("orgLon", orgLon);
			map.put("orgLat", orgLat);
			map.put("orgPcode", orgPcode);
			map.put("orgSort", orgSort);

			
			//2、查询组织机构信息表列表
			List<OrgInfo> orgInfoList = orgInfoService.findOrgInfoByPage(page,rows,map);
			log.info("orgInfoList={}",orgInfoList);
			List<OrgInfoVO> orgInfoVOList = orgInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<OrgInfoVO> result = new PageResult<OrgInfoVO>(orgInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param orgInfoVO
	 * @param orgInfo
	 * @return
	 */
	public void changeVOToPO(OrgInfoVO orgInfoVO,OrgInfo orgInfo){
			BeanUtils.copyProperties(orgInfoVO,orgInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param orgInfoVO
	 * @param orgInfo
	 * @return
	 */
	public void changePoToVO(OrgInfoVO orgInfoVO,OrgInfo orgInfo){
			BeanUtils.copyProperties(orgInfo,orgInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param orgInfo
	 * @return
	 */
	public OrgInfoVO changePoToVO(OrgInfo orgInfo){
			OrgInfoVO orgInfoVO=new OrgInfoVO();
			BeanUtils.copyProperties(orgInfo,orgInfoVO);
			

			return orgInfoVO;
	}

	/**
	 * 3.查询组织机构信息表--详情页
	 * @param orgId
	 * @return
	 */
	@RequestMapping("/queryForOrgInfoDetail")
	public ModelAndView queryForOrgInfoDetail(@RequestParam(value="orgId",required=false) String orgId){
			//1.校验id
			CommonUtils.validateExistID(orgId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("orgId", orgId);
			log.info("查询组织机构信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			OrgInfo orgInfo = orgInfoService.findByOrgInfoId(map);
			
			OrgInfoVO orgInfoVO = changePoToVO(orgInfo);
			//3、返回前台
			log.info("查询组织机构信息表查询成功：orgInfo={}",orgInfo);
			modelAndView.setViewName("/system/org/queryForOrgInfoDetail");
			modelAndView.addObject("orgInfoVO",orgInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}


	/**
	 * 6.删除组织机构信息表操作
	 * @param orgId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOrgInfoId")
	public JsonResultUtil deleteByOrgInfoId(@RequestParam(value="orgId",required=false) String orgId){
			//1.校验id
			CommonUtils.validateExistID(orgId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("orgId", orgId);
			orgInfoService.findByOrgInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除组织机构信息表入参，map={}",map);
			
			try{
				orgInfoService.upDelByOrgInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除组织机构信息表操作---批量删除
	 * @param orgIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByOrgInfoIdBatch")
	public JsonResultUtil deleteByOrgInfoIdBatch(@RequestParam(value="orgIdList",required=false) List orgIdList){
			//1.校验id
			CommonUtils.validateExistIDList(orgIdList);
			
			//2.校验数据库
			orgInfoService.findByOrgInfoIdList(orgIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("orgIdList", orgIdList);
			map.put("updateBy","000");
			log.info("批量删除组织机构信息表操作入参，map={}",map);
			
			try{
				orgInfoService.upDelByOrgInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}